$.extend({
    modalWindow:function (element, options) {
        var defaultOptions  =   {
            //modal窗口submit按钮提交后响应
            submitResponse:function (response) {
                if (!($.isPlainObject(response))) {
                    if(defaultOptions.is_alert){
                        $(modal.body).html(response);
                    }else{
                        $(modal.body).before(response);
                    }
                    return;
                }

                if(response.status == true){
                    $(alert_modal).addClass("alert-success");
                    $(alert_icon).addClass("fa-check-circle");
                    $(alert_message).html(response.message);
                    $(defaultOptions.body).html(alert_modal);
                    var reload  =   false, is_close_modal = false;
                    setTimeout(function () {
                        showLoading();
                        $.get(response.reloadUrl, function (data) {
                            modal.closeModal();
                            modal.closeOverlay();
                            $(".center-block").html(data.replace("<!DOCTYPE html>", ""));
                            //重新预加载JS事件
                            $.getScript("/js/ready.js");
                        });
                    },300);
                }else{
                    $(alert_modal).addClass("alert-danger");
                    $(alert_icon).addClass("fa-times-circle");
                    $(alert_message).html(response.message);
                    $(modal.body).before(alert_modal);
                    $(modal.sumbButton).removeAttr("disabled");
                }
            },
            //modal窗口submit按钮
            submitRequest:function (modalWindow, element) {
                var form = $(modal.mainWindow).find("form"), form_data = '', form_url = '', method = "POST";
                $("body").append(loading);
                if(!defaultOptions.is_alert && form){
                    form_data = $(form).serialize();
                    form_url = $(form).attr("action");
                }else {
                    method = "GET";
                    form_url = $(modal.sumbButton).attr("data-url");
                }
                $(modal.sumbButton).attr("disabled", 'true');
                $.ajax({
                    type: method,
                    url: form_url,
                    data: form_data,
                    success: defaultOptions.submitResponse,
                    error:function (response) {
                        $(modal.body).before(response);
                    }
                });
            },
            //loading动画
            loading:function () {
                var loading = $("<figure class='loading'></figure>");
                $(loading).append('<div class="dot white"></div>').append('<div class="dot"></div>')
                    .append('<div class="dot"></div>').append('<div class="dot"></div>')
                    .append('<div class="dot"></div>');
                return loading;
            },
            //打开modal加载的内容
            modalLoad:function (url, m, el) {
                $.get(url, function (data) {
                    $(m.body).html(data);
                    //重新预加载JS事件
                    $.getScript("/js/ready.js");
                })
            },
            is_alert:false,
        };

        $.each(options,function (key, value) {
            defaultOptions[key] = value;
        });

        //创建模态窗口
        var modal = {
            overlay : $('<div></div>').addClass('md-overlay'),
            mainWindow   : $("<div></div>").addClass("md-modal"),
            content : $("<div></div>").addClass("md-content"),
            header  : $("<div></div>").addClass("modal-header"),
            title   : $("<h4></h4>").addClass("modal-title"),
            buttonClose  : $("<button>x</button>").addClass("md-close close"),
            body    : $("<div></div>").addClass("modal-body"),
            footer  : $("<div></div>").addClass("modal-footer"),
            sumbButton      : $("<div></div>").addClass("btn btn-primary md-submit").html("确认"),
            cannelButton    : $("<button></button>").addClass("btn btn-danger md-close").html("取消"),

            closeModal : function () {
                $(modal.mainWindow).removeClass("md-show");
                setTimeout(function () {
                    modal.mainWindow.remove();
                }, 300);
            },

            closeOverlay : function (fn) {
                $(modal.overlay).removeClass("md-overlay-show");
                $(loading).removeClass("loading-show");
                $("#theme-wrapper").removeClass("blur");
                setTimeout(function () {
                    modal.overlay.remove();
                    $('body').css("overflow",'auto');
                    loading.remove();
                    //移除日期对话框
                    // modal.find(".datepicker").remove();
                    if (typeof(fn) == "function") {
                        fn();
                    }
                }, 300);
            },

            showModal : function () {
                var string = "md-effect-1,md-effect-3,md-effect-4,md-effect-5,md-effect-8,md-effect-9,md-effect-10,md-effect-11," +
                    "md-effect-13,md-effect-14,md-effect-15";
                var array = string.split(",");
                var value = array[Math.round(Math.random() * (array.length - 1))];
                console.log(value);
                $(modal.mainWindow).addClass(value);
                setTimeout(function () {
                    $(modal.mainWindow).addClass("md-show");
                    $(modal.overlay).addClass("md-overlay-show");
                    $('body').css("overflow",'hidden');
                }, 10);


            }
        };

        //给背景添加虚化
        $("#theme-wrapper").addClass("blur");
        //信息提示
        var alert_modal   =   $("<div></div>").addClass("alert fade in");
        var alert_message = $('<strong>Response Message!</strong>');
        var alert_icon = $('<i class="fa fa-fw fa-lg"></i>');
        $(alert_modal).append(alert_icon);
        $(alert_modal).append(alert_message);
        //loading动画
        var loading = defaultOptions.loading();

        modal.content.append(modal.header);
        modal.title.html('Modal title');
        modal.header.append(modal.buttonClose);
        modal.header.append(modal.title);
        modal.content.append(modal.body);
        modal.content.append(modal.footer);
        modal.mainWindow.append(modal.content);
        modal.footer.append(modal.sumbButton);
        $("body").append(modal.overlay);
        $("body").append(modal.mainWindow);

        var showLoading = function () {
            $("body").append(loading);
            $(loading).addClass("loading-show");
        };

        $(modal.overlay).dblclick(function () {
            modal.closeModal();
            modal.closeOverlay();
        });

        $(modal.buttonClose).click(function () {
            modal.closeModal();
            modal.closeOverlay();
        });

        $(modal.sumbButton).click(function () {
            defaultOptions.submitRequest(modal, element);
        })

        var data_html = $(element).attr("data-html");
        var url = $(element).attr("data-url");
        var data_title = $(element).attr("data-title");
        var submit = $(element).attr("data-submit");

        if (data_title) {
            $(modal.title).html(data_title);
            $(modal.sumbButton).html(submit);
        } else {
            $(modal.title).html("Model Title");
            $(modal.sumbButton).html("Submit");
        }

        if(defaultOptions.is_alert){
            modal.footer.append(modal.cannelButton);
            $(modal.body).html(data_html);
            $(modal.sumbButton).attr("data-url", url);
            $(modal.body).addClass('modal-body-alert');
            $(modal.cannelButton).click(function () {
                modal.closeModal();
                modal.closeOverlay();
            });
        }else{
            defaultOptions.modalLoad(url, modal, element);
        }

        modal.showModal();
    }
});
